//
//  ViewController.swift
//  CustomFont
//
//  Created by winter on 16/8/21.
//  Copyright © 2016年 wd. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var changeFontButton: UIButton!
    
    var data = ["this for test", "这个还是测试", "再来个测试", "好吧，这个是最后一个测试", "winter"]
    var fontNames : [String] = []
    var fontName = String()
    
    @IBAction func changeFontButtonAction(sender: UIButton) {
        let count = fontNames.count
        let idx = Int(arc4random_uniform(UInt32(count)))
        fontName = fontNames[idx]
        tableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        changeFontButton.layer.cornerRadius = 50
        
        fontNames = UIFont.familyNames()
        fontName = fontNames[0]
        
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        
        cell.textLabel?.text = data[indexPath.row]
        cell.textLabel?.font = UIFont.init(name: fontName, size: 16)
        return cell
    }
}